/* Copyright (c) 2010, Peter Lényi */
/* Licensed under The BSD License. */

package gem.apps.fs.web;

import gem.apps.fs.engine.FileServer;
import gem.apps.fs.entities.FileMeta;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet class FileDetailsPreServlet retrieves metadata about a file to be
 * displayed in a jsp and turns control over to the jsp.
 *
 * @author Peter Lényi
 * @version 20100425F
 */
public class FileDetailsPreServlet extends HttpServlet
{
    private static final String ADDRESS_FATAL = "/error/fatal/jsp";
    private static final String ADDRESS_SUCCESS = "/file/details/jsp";
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // prepare resources
        FileServer fs = FileServer.getInstance();

        // load request parameters
        String uinString = request.getParameter("uin");

        // execute logic
        try {
            long uin = Long.parseLong(uinString);
            FileMeta filemeta = fs.getFileMeta(uin);

            // pass connection
            request.getSession().setAttribute("filemeta", filemeta);
            response.sendRedirect(getServletContext().getContextPath() + ADDRESS_SUCCESS + "?uin=" + uin);
        }
        catch (NumberFormatException ex) {
            response.sendRedirect(getServletContext().getContextPath() + ADDRESS_FATAL);
        }
    }
}
